{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d38c5516",
   "metadata": {},
   "outputs": [],
   "source": [
    "#　思考：能否将一些功能作函数的封装，实现更加便捷的API调用\n",
    "def face_detect(API_key,API_secret,image_url,landmark=None,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    \"\"\"face++ 人脸检测接口函数(图片链接)\"\"\"\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'image_url':image_url,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes,\n",
    "        'face_rectangle':face_rectangle,\n",
    "        'beauty_score_min':beauty_score_min,\n",
    "        'beauty_score_max':beauty_score_max   \n",
    "    }\n",
    "    r = requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2798002c",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'API_key' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-caf92b96e4b3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mface_detect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mAPI_key\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mAPI_secret\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'http://thumb10.jfcdns.com/thumb/up/2017-8/201782115518958221215010_600_0.jpg'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'API_key' is not defined"
     ]
    }
   ],
   "source": [
    "face_detect(API_key,API_secret,'http://thumb10.jfcdns.com/thumb/up/2017-8/201782115518958221215010_600_0.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0a07b47f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "# 2.输入我们需要API（face++）网站注册的API_Key\\Secret\n",
    "\n",
    "API_Key = \"aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW\"\n",
    "API_Secret = \"wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC\"\n",
    "\n",
    "# 3.准备base URL(face detect) API测试接口的url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":\"http://thumb10.jfcdns.com/thumb/up/2017-8/201782115518958221215010_600_0.jpg\",\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a7958cc2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c9ea3435",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1647581964,0f620440-2f49-4c6b-84c7-c64f1ccf9533\",\"time_used\":343,\"faces\":[{\"face_token\":\"cced722224d8688284c8e098c362d036\",\"face_rectangle\":{\"top\":132,\"left\":178,\"width\":174,\"height\":174}}],\"image_id\":\"iGkOO0vIVdOtC8bDQWAVmA==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f145c55d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647580322,710cdb54-59a9-4605-bbf5-15f4ab50d8b4',\n",
       " 'time_used': 444,\n",
       " 'faces': [{'face_token': '731bddf550b804ce8b48d3dfa92f000c',\n",
       "   'face_rectangle': {'top': 132, 'left': 178, 'width': 174, 'height': 174}}],\n",
       " 'image_id': 'iGkOO0vIVdOtC8bDQWAVmA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "21664dae",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'faceset_token' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-7c8c05762aea>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;33m[\u001b[0m\u001b[0mfaceset_token\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'faceset_token' is not defined"
     ]
    }
   ],
   "source": [
    "[faceset_token]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3276af07",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "# 2.输入我们需要API（face++）网站注册的API_Key\\Secret\n",
    "\n",
    "API_Key = \"aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW\"\n",
    "API_Secret = \"wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC\"\n",
    "\n",
    "# 3.准备base URL(face detect) API测试接口的url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_token\":\"ce2c77d7c093ebbd320a8bdfa7ed8340\",\n",
    "    \"outer_id\":\"facesetid\"\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d4647ccd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa956bf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3c6beae7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "# 2.输入我们需要API（face++）网站注册的API_Key\\Secret\n",
    "\n",
    "API_Key = \"aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW\"\n",
    "API_Secret = \"wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC\"\n",
    "\n",
    "# 3.准备base URL(face detect) API测试接口的url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_tokens\":\"ce2c77d7c093ebbd320a8bdfa7ed8340​,cced722224d8688284c8e098c362d036\",\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "61ca5201",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "24a407c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '619ea6aa135915d8bc904600704853ad',\n",
       " 'time_used': 585,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1647582290,4dd261db-ed36-4977-812d-eab0c085218b',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': [{'reason': 'INVALID_FACE_TOKEN',\n",
       "   'face_token': 'ce2c77d7c093ebbd320a8bdfa7ed8340\\u200b'}]}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "686a0944",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "# 2.输入我们需要API（face++）网站注册的API_Key\\Secret\n",
    "\n",
    "API_Key = \"aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW\"\n",
    "API_Secret = \"wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC\"\n",
    "\n",
    "# 3.准备base URL(face detect) API测试接口的url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_tokens\":\"ce2c77d7c093ebbd320a8bdfa7ed8340\",\n",
    "    \"faceset_token\":\"619ea6aa135915d8bc904600704853ad\"\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "7d0a9180",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2141c7ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '74b253f5c71e09109f7a46bfa9d0a4ba',\n",
       " 'time_used': 172,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1647582366,967dfd72-39f2-4903-a77f-5f465f22b67b',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': [{'reason': 'INVALID_FACE_TOKEN',\n",
       "   'face_token': 'ce2c77d7c093ebbd320a8bdfa7ed8340'}]}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "9d83c580",
   "metadata": {},
   "outputs": [],
   "source": [
    "#　思考：能否将一些功能作函数的封装，实现更加便捷的API调用\n",
    "def face_detect(API_key,API_secret,image_url,landmark=None,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    \"\"\"face++ 人脸检测接口函数(图片链接)\"\"\"\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload={\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'image_url':image_url,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes,\n",
    "        'face_rectangle':face_rectangle,\n",
    "        'beauty_score_min':beauty_score_min,\n",
    "        'beauty_score_max':beauty_score_max   \n",
    "    }\n",
    "    r = requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "8b0f3caa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647583643,c62098d0-17ce-43a7-8cdc-854f8a0122a9',\n",
       " 'time_used': 192,\n",
       " 'faces': [{'face_token': '8ec763bfa3d40c25d938dcaaa9ca9982',\n",
       "   'face_rectangle': {'top': 132, 'left': 178, 'width': 174, 'height': 174}}],\n",
       " 'image_id': 'iGkOO0vIVdOtC8bDQWAVmA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_detect(\"aZW8w-JCfLUxYnTwQQu_RKLAUduXKvTW\",\"wmAcTGRDAkP1JB4zYe0H3Q15mn1UCMxC\",'http://thumb10.jfcdns.com/thumb/up/2017-8/201782115518958221215010_600_0.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a49220be",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
